package com.push.web.spider;

import com.push.common.utils.DateUtils;
import com.push.web.domain.BizBibExpert;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;

public class WanFangSpider {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static int sleepTime = 0;

    private BizBibExpert bizBibExpert;

    private String wfBaseUrl = "https://s.wanfangdata.com.cn/advanced-search/paper";

    public static void main(String[] args){
        BizBibExpert bizBibExpert = new BizBibExpert();
        bizBibExpert.setExpName("王衍喜");
        bizBibExpert.setExpAffi("中国科学院软件研究所");
        bizBibExpert.setStartTime("2011");
        bizBibExpert.setEndTime("2022");
        WanFangSpider wanFangSpider = new WanFangSpider(bizBibExpert);
        try {
            wanFangSpider.run();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public WanFangSpider() {
    }

    public WanFangSpider(BizBibExpert bizBibExpert) {
        this.bizBibExpert = bizBibExpert;
    }

    public void run() throws InterruptedException, ParseException {
        WebDriverManager.firefoxdriver().setup();
        WebDriver driver = new FirefoxDriver();
        driver.get(wfBaseUrl);
        Thread.sleep(sleepTime);
        //选择作者
        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[1]/div/div[@class='search-option field ivu-select ivu-select-single ivu-select-default']/div[@class='ivu-select-selection']/div/span[@class='ivu-select-selected-value']")).click();
        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[1]/div/div[@class='search-option field ivu-select ivu-select-visible ivu-select-single ivu-select-default']/div/ul/li[5]")).click();
        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[1]/div/div/input")).sendKeys(bizBibExpert.getExpName());

        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[2]/div/div[@class='search-option field ivu-select ivu-select-single ivu-select-default']/div[@class='ivu-select-selection']/div/span[@class='ivu-select-selected-value']")).click();
        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[2]/div/div[@class='search-option field ivu-select ivu-select-visible ivu-select-single ivu-select-default']/div/ul/li[6]")).click();
        driver.findElement(By.xpath("//div[@class='advanced-search-box']/div[@class='search-input']/div/div[1]/div[2]/div/div/input")).sendKeys(bizBibExpert.getExpAffi());
        //为了避免出现不可点击的情况发生，需要遍历年份
        int startDiff = DateUtils.diffYear(DateUtils.getDate(), bizBibExpert.getStartTime())+1;
        for (int i = 0; i < startDiff; i++){
            System.out.println((i + 2));
            driver.findElement(By.xpath("//div[@class='advanced-search-box']/div/div[1]/div/div/span")).click();
            Thread.sleep(1000);
            driver.findElement(By.xpath("//div[@class='advanced-search-box']/div/div[@class='ivu-select ivu-select-visible ivu-select-single ivu-select-default']/div/ul/li["+(i + 2)+"]")).click();
            Thread.sleep(1000);
        }
        int endDiff = DateUtils.diffYear(DateUtils.getDate(), bizBibExpert.getEndTime()) +1;
        for (int i = 0; i < endDiff; i++){
            driver.findElement(By.xpath("//div[@class='advanced-search-box']/div/div[2]/div/div/span")).click();
            Thread.sleep(1000);
            driver.findElement(By.xpath("//div[@class='advanced-search-box']/div/div[@class='ivu-select ivu-select-visible ivu-select-single ivu-select-default']/div/ul/li["+(i + 2)+"]")).click();
            Thread.sleep(1000);
        }
        driver.findElement(By.xpath("//div[@class='submit']/span[@class='submit-btn']")).click();
    }
}
